package org.agiletestplus.server;

import java.util.List;

import org.agiletestplus.client.objects.Configuration;
import org.agiletestplus.client.objects.ConfigurationType;
import org.damour.base.server.hibernate.helpers.DefaultData;
import org.hibernate.Session;

public class TestDefaultData extends DefaultData {
  @SuppressWarnings("unchecked")
  public void create(Session session) {
    super.create(session);
    List<ConfigurationType> configTypes = session.createQuery("from ConfigurationType where name = 'OS'").setCacheable(true).list();
    if (configTypes.size() == 0) {
      // define a set of default configuration types and configurations
      ConfigurationType os = createConfigType(session, "OS", "Operating System");
      createConfig(session, os, "Windows XP 32bit", "Windows XP 32bit");
      createConfig(session, os, "Windows XP 64bit", "Windows XP 64bit");
      createConfig(session, os, "Windows 7 32bit", "Windows 7 32bit");
      createConfig(session, os, "Windows 7 64bit", "Windows 7 64bit");
      createConfig(session, os, "Ubuntu 32bit", "Ubuntu Linux 32bit");
      createConfig(session, os, "Ubuntu 64bit", "Ubuntu Linux 64bit");
      createConfig(session, os, "MacOS", "Mac OS X");
      
      ConfigurationType browser = createConfigType(session, "Browser", "Web Browser");
      createConfig(session, browser, "FF3.6", "Firefox 3.6");
      createConfig(session, browser, "FF4", "Firefox 4");
      createConfig(session, browser, "FF5", "Firefox 5");
      createConfig(session, browser, "IE7", "Internet Explorer 7");
      createConfig(session, browser, "IE8", "Internet Explorer 8");
      createConfig(session, browser, "IE9", "Internet Explorer 9");

      ConfigurationType database = createConfigType(session, "Database", "Database");
      createConfig(session, database, "MySQL 5.0", "MySQL 5.0");
      createConfig(session, database, "Oracle 11g", "Oracle 11g");
      createConfig(session, database, "Oracle 10g", "Oracle 10g");
      createConfig(session, database, "Postgres 8.3", "Postgres 8.3");
      createConfig(session, database, "Postgres 8.4", "Postgres 8.4");
    }
  }
  protected ConfigurationType createConfigType(Session session, String name, String description) {
    ConfigurationType configType = new ConfigurationType();
    configType.setName(name);
    configType.setDescription(description);
    session.save(configType);
    return configType;
  }
  
  protected Configuration createConfig(Session session, ConfigurationType parent, String name, String description) {
    Configuration config = new Configuration();
    config.setParent(parent);
    config.setName(name);
    config.setDescription(description);
    session.save(config);
    return config;
  }
}
